package adim;

import javax.microedition.lcdui.*;

public class NumericTextBox extends TextBox
{
	private int _constraint, _length;
	private boolean _strictLength;

	public NumericTextBox(String title, String initstr, int length, int constraint)
	{
		super(title, initstr, length, constraint);
		_length = length;
		_strictLength = false;
	}

	public void setStrictLength()
	{
		_strictLength = true;
	}

	public boolean verify()
	{
		boolean _ok_length = false;
		boolean _ok_constraint = false;

		if(_strictLength) {
			_ok_length = _verifyLength();
		} else {
			_ok_length = true;
		}

		_ok_constraint = _verifyConstraint();

		return (_ok_constraint && _ok_length);
	}

	private boolean _verifyLength()
	{
		String l = this.getString();
		if (l.length() == this._length) {
			return true;
		} 

		return false;
	}

	private boolean _verifyConstraint()
	{
		String l = this.getString();
		char[] larray = l.toCharArray();

		for(int i = 0; i < l.length(); i++) {
			if (larray[i] < '0' || larray[i] > '9') {
				return false;
			}
		}
		
		return true;
	}
}
